package views.subscribe
{
	import commands.customer.CreateCustomerCmd;
	import commands.user.CreateUserCmd;
	
	import cultu.community.message.Community;
	import cultu.core.message.Address;
	import cultu.core.message.Role;
	import cultu.customer.message.Customer;
	
	import flash.events.MouseEvent;
	
	import model.Model;
	
	import renderers.TreeCommunityRenderer;
	
	import utils.CultuConstants;
	import utils.CultuEvent;
	
	import vo.CustomerVO;
	import vo.UserVO;
	import vo.community.CommunityVO;
	
	import wizeoo.events.WIZCommandEvent;
	import wizeoo.interfaces.WIZPilotAncestor;
	import wizeoo.masters.MasterPilot;
	import wizeoo.models.WIZGlobalParameters;
	
	public class SubscribeViewPilot extends WIZPilotAncestor
	{
		
		/**Référence au model*/
		private var mod:Model = Model.getInstance();
		
		[Bindable]  
		override public var masterPilot: MasterPilot;
		
		public var view:SubscribeView;
		
		public function SubscribeViewPilot()
		{
			super();
		}
		
		override public function wizCreation():void
		{
			view=masterPilot.linkedView as SubscribeView;
		}
		
		override public function wizSynchronizeData():void
		{
			view.treeCommunities.dataProvider = mod.communitiesCollection;
		}
		
		override public function wizAddListeners():void
		{
			view.treeCommunities.addEventListener(CultuEvent.ADD_PREFERENCE,onAddPreference,false,0,true);
			view.treeCommunities.addEventListener(CultuEvent.REMOVE_PREFERENCE,onRemovePreference,false,0,true);
			view.btCreate.addEventListener(MouseEvent.CLICK,onBtCreateClick,false,0,true);
		}
		
		override public function wizRemoveListeners():void
		{
			view.treeCommunities.removeEventListener(CultuEvent.ADD_PREFERENCE,onAddPreference);
			view.treeCommunities.removeEventListener(CultuEvent.REMOVE_PREFERENCE,onRemovePreference);
			view.btCreate.removeEventListener(MouseEvent.CLICK,onBtCreateClick);
		}
		
		override public function wizRefresh():void
		{
			
		}
		
		override public function wizExit():void
		{
			
		}
		
		private function onAddPreference(e:CultuEvent):void
		{
			
			
			if(!e && !e.data)
				return;
			
			if(!mod.customerCreate)
				mod.customerCreate = new CustomerVO(null);
			
			var pCom:CommunityVO = (e.data as TreeCommunityRenderer).community;
			
			mod.customerCreate.addPreference(pCom,CultuConstants.CHOOSED_PREFERENCE_SCORE);
			
			//vérifier que l'univers fait bien parti des préf users
			if(!pCom.isUniverse())
			{
				if(mod.customerCreate.containsPreference(mod.communitiesDic[pCom.universe] as CommunityVO) == -1)
				{
					mod.customerCreate.addPreference(mod.communitiesDic[pCom.universe] as CommunityVO,CultuConstants.CHOOSED_PREFERENCE_SCORE);
				}
			}
			
			WIZGlobalParameters.root.dispatchEvent(new CultuEvent(CultuEvent.REFRESH_TREE_RENDERER));
		}
		
		private function onRemovePreference(e:CultuEvent):void
		{
			if(!e && !e.data)
				return;
			
			if(!mod.customerCreate)
				mod.customerCreate = new CustomerVO(null);
			
			mod.customerCreate.removePreference((e.data as TreeCommunityRenderer).community);
			
			WIZGlobalParameters.root.dispatchEvent(new CultuEvent(CultuEvent.REFRESH_TREE_RENDERER));
		}
		
		private function onBtCreateClick(e:MouseEvent):void
		{
			if(validateInfos())
			{
				mod.customerCreate.firstname = view.tiFirstname.text;
				mod.customerCreate.lastname = view.tiLastname.text;
				mod.customerCreate.mailable = view.cbMail.selected;
				mod.customerCreate.coordsusable = view.cbGeneral.selected;
				mod.customerCreate.promotionable = view.cbCommercial.selected;
				mod.customerCreate.address.addressLine1 = view.tiAddress.text;
				mod.customerCreate.address.country = 'France';
				mod.customerCreate.address.zipCode = view.tiZipCode.text;
				mod.customerCreate.address.town = view.tiTown.text;
				
				mod.userCreate = new UserVO(null);
				mod.userCreate.login = view.tiMail.text;
				mod.userCreate.password = view.tiPassword.text;
				mod.userCreate.role = mod.roleDic[CultuConstants.ROLE_CUSTOMER] as Role;
				mod.userCreate.customer = mod.customerCreate.srcCustomer;
				
				new CreateCustomerCmd(mod.customerCreate,onCustomerCreate).execute();
			}
		}
		
		/**retour sur création customer*/
		private function onCustomerCreate(pEvent:WIZCommandEvent):void
		{
			if(pEvent && pEvent.type == WIZCommandEvent.COMMAND_SUCCES)
			{
				mod.userCreate.customer = (pEvent.currentTarget as CreateCustomerCmd).customer.srcCustomer;
				new CreateUserCmd(mod.userCreate,onUserCreate).execute();
			}
		}
		
		/**retour de création user*/
		private function onUserCreate(pEvent:WIZCommandEvent):void
		{
			if(pEvent && pEvent.type == WIZCommandEvent.COMMAND_SUCCES)
			{
				mod.userCreate = null;
				mod.customerCreate = null;
				WIZGlobalParameters.root.dispatchEvent(new CultuEvent(CultuEvent.NAVIGATE, 'MAINVIEW'));
			}
		}
		
		private function validateInfos():Boolean
		{
			var result:Boolean = true;
			
			if(!view.tiMail.text)
			{
				result = false;
				view.tiMail.errorString = 'Merci de saisir une adresse e-mail valide';
			}
			
			if(!view.tiPassword.text)
			{
				result = false;
				view.tiPassword.errorString = 'Merci de saisir un mot de passe';
			}
			
			return result;
		}
	}
}